<div class="layui-fluid layui-anim sys-anim" id="sys-httptrace" lay-title="请求追踪">
    <div class="layui-row sys-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body sys-table-full">
                    <form class="layui-form layui-table-form" lay-filter="httptrace-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md11">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">请求方法</label>
                                        <div class="layui-input-inline">
                                            <select name="method">
                                                <option value=""></option>
                                                <option value="GET">GET</option>
                                                <option value="POST">POST</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">请求URL</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="url" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md1 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
                                    <i class="layui-icon">&#xe848;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                                    <i class="layui-icon">&#xe79b;</i>
                                </div>
                            </div>
                        </div>
                    </form>
                    <span class="sys-alert-base sys-alert-success">共追踪到 <strong id="count">0</strong> 条近期HTTP请求记录</span>
                    <table lay-filter="httptraceTable" lay-data="{id: 'httptraceTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="httptrace-method">
    <div>
        {{# if (d.method === 'GET') { }}
        <span class="layui-badge sys-bg-green">GET</span>
        {{# } else if (d.method === 'POST') { }}
        <span class="layui-badge sys-bg-blue">POST</span>
        {{# } else { }}
        <span class="layui-badge sys-bg-purple">{{d.method}}</span>
        {{# } }}
    </div>
</script>
<script type="text/html" id="httptrace-status">
    <div>
        {{# if (d.status < 200) { }}
        <span class="layui-badge sys-tag-purple">{{d.status}}</span>
        {{# } else if (d.status < 201) { }}
        <span class="layui-badge sys-tag-green">{{d.status}}</span>
        {{# } else if (d.status < 399) { }}
        <span class="layui-badge sys-tag-cyan">{{d.status}}</span>
        {{# } else if (d.status < 403) { }}
        <span class="layui-badge sys-tag-orange">{{d.status}}</span>
        {{# } else if (d.status < 501) { }}
        <span class="layui-badge sys-tag-red">{{d.status}}</span>
        {{# } else { }}
        <span class="layui-badge sys-bg-geekblue">{{d.status}}</span>
        {{# } }}
    </div>
</script>
<script type="text/html" id="httptrace-time">
    <div>
        {{# if (d.timeTaken < 500) { }}
        <span class="layui-badge sys-tag-green">{{d.timeTaken}} ms</span>
        {{# } else if (d.timeTaken < 1000) { }}
        <span class="layui-badge sys-tag-cyan">{{d.timeTaken}} ms</span>
        {{# } else if (d.timeTaken < 1500) { }}
        <span class="layui-badge sys-tag-orange">{{d.timeTaken}} ms</span>
        {{# } else { }}
        <span class="layui-badge sys-tag-red">{{d.timeTaken}} ms</span>
        {{# } }}
    </div>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['jquery', 'table', 'sys', 'form'], function () {
        var $ = layui.jquery,
            table = layui.table,
            sys = layui.sys,
            $view = $('#sys-httptrace'),
            $searchForm = $view.find('form'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            form = layui.form,
            tableIns;

        form.render();
        initTable();

        $query.on('click', function () {
            tableIns.reload({where: getQueryParams(), page: {curr: 1}});
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            tableIns.reload({where: getQueryParams(), page: {curr: 1}});
        });

        function initTable() {
            tableIns = sys.table.init({
                elem: $view.find('table'),
                page: false,
                id: 'httptraceTable',
                url: ctx + 'sys/actuator/httptrace',
                cols: [[
                    {field: 'requestTime', title: '请求时间', minWidth: 180},
                    {title: '请求方法', templet: "#httptrace-method"},
                    {field: 'url', title: '请求URL', minWidth: 380},
                    {title: '响应状态', templet: '#httptrace-status'},
                    {title: '请求耗时', templet: '#httptrace-time'}
                ]],
                done: function (r) {
                    $view.find('#count').text(r.count);
                }
            });
        }

        function getQueryParams() {
            return {
                method: $searchForm.find('select[name="method"]').val(),
                url: $searchForm.find('input[name="url"]').val().trim(),
                invalidate_ie_cache: new Date()
            };
        }
    });
</script>